﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using AimGameEngine.EditorObject;

using AimGameLibrary;
using AimGameLibrary.GameObject;

namespace AimGameEngine.NodeEditorObject
{
    public class NodeAnimation2D : NodeBaseControl
    {
        public KeyFrameData MyKeyFrameData;
        public FrameData MyFrameData;
        public NodeTexture2D texture2d;
        public int myint;
        public NodeAnimation2D(SpriteBatch inputspritebatch, Camera inputcamera, NodeData inputData
            , List<IDraw> MyListDraw, List<IUpdateMainTime> MyListUpdateTime, Dictionary<String, NodeObject> MyListObject, Dictionary<String, NodeObject> MyListType)
            : base(inputspritebatch, inputcamera, inputData, MyListDraw,MyListUpdateTime, MyListObject, MyListType)
        {
            MyKeyFrameData = new KeyFrameData();
            MyFrameData = new FrameData();

            InputMode = NodeInputMode.None;

            ClearConnecter();

            Connecter outputRectangle = new Connecter(inputspritebatch, inputcamera, Syco.Output, NodeTypeValue.Rectangle, this);
            ListConnectpoint.Add(outputRectangle);

            Connecter outputUpdate = new Connecter(inputspritebatch, inputcamera, Syco.Output, NodeTypeValue.Update, this);
            ListConnectpoint.Add(outputUpdate);

            Connecter inputTexture = new Connecter(inputspritebatch, inputcamera, Syco.Input, NodeTypeValue.Texture2D, this);
            ListConnectpoint.Add(inputTexture);

           // Connecter inputInt = new Connecter(inputspritebatch, inputcamera, Syco.Input, NodeTypeValue.Int, this);
           // ListConnectpoint.Add(inputInt);
        }

        public override void SetupInput(Connecter Thisconnect, Connecter connectOutput)
        {
            /// One Input
            if (connectOutput.MyType == NodeTypeValue.Texture2D)
            {
                MyKeyFrameData.MyData = connectOutput.MyNode.MyData;
                texture2d = (NodeTexture2D)connectOutput.MyNode;
            }
            else if (connectOutput.MyType == NodeTypeValue.Int)
            {
                this.MyData.Data = connectOutput.MyNode.MyData.MyName;
            }
        }

        public override void ClearInput(Connecter ThisInputConnect, Connecter ThisConnectClear, string NameThisClear)
        {
            //base.ClearInput(ThisConnectClear, NameThisClear);
            if (ThisConnectClear.MyType == NodeTypeValue.Texture2D)
            {
                MyKeyFrameData.MyData = null;
                texture2d = null;
            }
            else if (ThisConnectClear.MyType == NodeTypeValue.Int)
                MyData.Data.Replace(NameThisClear, "NULL");
        }

        public override void ShowProprety(ArrayList Temp, CallBack input)
        {
            base.ShowProprety(Temp, input);
            ChackSource();
            PropretyAnimation2D animation2dproprety = new PropretyAnimation2D(this);
            animation2dproprety.okButton.Click += new EventHandler(input.Deactivate);
            //animation2dproprety.Deactivate += new EventHandler(input.Deactivate);
            animation2dproprety.ShowDialog();
            
        }

        public override void SetupProprety()
        {
            base.SetupProprety();

            Connecter inputTexture = new Connecter(spritebatch, camera, Syco.Input, NodeTypeValue.Texture2D, this);
            ListConnectpoint.Add(inputTexture);

            if (InputMode == NodeInputMode.Int_1)
            {
                Connecter inputInt = new Connecter(this.spritebatch, this.camera, Syco.Input, NodeTypeValue.Int, this);
                ListConnectpoint.Add(inputInt);
            }

            this.AfterSetupProprety();
        }

        private void ChackSource()
        {
            //if (MyKeyFrameData.MyData != null)
            //{
            //    if (MyKeyFrameData.MyData.Data != null && MyKeyFrameData.MyData.Data != "NULL") // Check file Path None build
            //        if (File.Exists(Script.SettingFile))
            //        {
            //            XMLData<SettingEditorData> xmlSetEditor = new XMLData<SettingEditorData>();
            //            SettingEditorData settingEditor = xmlSetEditor.Load(Script.SettingFile);

            //            // None Check File to File
            //            if (File.Exists(settingEditor.BuildOutput + "\\Content\\" + MyKeyFrameData.MyData.MyName + ".xnb")) // Check file xnb which have
            //            {
            //                //Stream fs = se.container.OpenFile(MyKeyFrameData.MyData.Data, FileMode.Open); 
            //                //Texture2D.FromFile(
                            
            //                MyFrameData.Width = Texture2D.FromFile(spritebatch.GraphicsDevice, MyKeyFrameData.MyData.Data).Width;
            //                MyFrameData.Height = Texture2D.FromFile(spritebatch.GraphicsDevice, MyKeyFrameData.MyData.Data).Height;
            //            }
            //        }
            //}
            if (texture2d != null)
            {
                Texture2D temptexture2d = texture2d.GetTexture();
                if (temptexture2d != null)
                {
                    MyFrameData.Width = temptexture2d.Width;
                    MyFrameData.Height = temptexture2d.Height;
                }
                else
                {
                    MyFrameData.Width = 0;
                    MyFrameData.Height = 0;
                }
            }
            else
            {
                MyFrameData.Width = 0;
                MyFrameData.Height = 0;
            }
            
        }

        protected override Dictionary<string, NodeObject> GetDictionaryNode(Dictionary<string, NodeObject> TempNode)
        {
            if(this.MyData.Data != null)
            if (this.MyDictionaryObject.ContainsKey(this.MyData.Data))
                TempNode.Add(this.MyData.Data, this.MyDictionaryObject[this.MyData.Data]);

            return base.GetDictionaryNode(TempNode);
        }

        public override void SaveData(System.Collections.ArrayList Thislist, SaveMode inputsavemode)
        {
            base.SaveData(Thislist,inputsavemode);

            Thislist.Add(InputMode);

            // MyData have Data NameInt
            if (texture2d != null)
                Thislist.Add(texture2d.MyData.MyName);
            else Thislist.Add("NULL");

            if (MyFrameData != null)
                Thislist.Add(MyFrameData);
            else Thislist.Add("NULL");

            if (MyKeyFrameData != null)
                Thislist.Add(MyKeyFrameData);
            else Thislist.Add("NULL");

            Thislist.Add(myint);
        }

        public override void LoadData(Identification getdata, int IndexData)
        {
            base.LoadData(getdata, IndexData);

            if (getdata.Items.GetValue(IndexData + 1).GetType() == typeof(NodeInputMode))
            InputMode = (NodeInputMode)getdata.Items.GetValue(IndexData + 1);

            if(getdata.Items.GetValue(IndexData + 3).GetType() == typeof(FrameData))
            MyFrameData = (FrameData)getdata.Items.GetValue(IndexData + 3);

            if(getdata.Items.GetValue(IndexData + 4).GetType() == typeof(KeyFrameData))
            MyKeyFrameData = (KeyFrameData)getdata.Items.GetValue(IndexData + 4);

            SetupProprety();
        }
    }
}
